package cn.seaboot.admin.quartz.web;

import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.quartz.bean.ScheduleAction;
import cn.seaboot.admin.quartz.bean.ScheduleJob;
import cn.seaboot.admin.quartz.service.ScheduleActionService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 定时请求任务 [t_schedule_action] Controller
 *
 * @author Mr.css on 2018-6-20
 */
@Controller
@Tag(name = "定时请求任务 [t_schedule_action] 管理")
@RequestMapping("scheduleAction")
public class ScheduleActionCtrl {

    @Resource
    private ScheduleActionService scheduleActionService;

    /**
     * 定时请求任务 - 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "定时请求任务 [t_schedule_action] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated ScheduleJob job, @Validated ScheduleAction pojo) throws SchedulerException {
        return scheduleActionService.insert(job, pojo);
    }

    /**
     * 定时请求任务 - 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "定时请求任务 [t_schedule_action] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params Long id) {
        return scheduleActionService.deleteById(id);
    }

    /**
     * 定时请求任务 - 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "定时请求任务 [t_schedule_action] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public ScheduleAction queryById(@Params Long id) {
        return scheduleActionService.queryById(id);
    }

    /**
     * 定时请求任务 - 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "定时请求任务 [t_schedule_action] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Validated ScheduleAction pojo, @Params Long id) {
        return scheduleActionService.updateById(pojo);
    }

    /**
     * 定时请求任务 - 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @Operation(description = "定时请求任务 [t_schedule_action] 列表查询")
    @Parameters({@Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数"),
            @Parameter(name = "sort", description = "排序")})
    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<ScheduleAction> queryPage(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map -> scheduleActionService.queryList(map));
    }
}
